//
//  YGStatusViewModel.swift
//  sinaWeiBoGui
//
//  Created by liyonggui on 16/5/15.
//  Copyright © 2016年 liyonggui. All rights reserved.
//

import UIKit

class YGStatusViewModel: NSObject {
    
    /// 单例
    static let sharedInstance: YGStatusViewModel = YGStatusViewModel()
    
    private var isLoadingStatus = false
    
    // 重构私有方法init, 防止外面调用
    private override init() {
        super.init()
    }
    
    /// 加载微博数据
    // access_token
    func loadStatus(since_id: Int64, max_id: Int64, loadStatusCallBack: (statuses: [YGStatus]?, error: NSError?) -> ()) {
        
        if isLoadingStatus {
            let error = NSError(domain: "cn.itcast.error.repeatloadstatus", code: -1, userInfo: ["errorDescription" : "有人正在加载微博数据,直接返回,请下次在来"])
            loadStatusCallBack(statuses: nil, error: error)
            return
        }
        
        // 加载数据
        isLoadingStatus = true
        
        guard let access_token = YGUserAccountViewModel.sharedInstance.userAccount?.access_token else {
            YGPrint(items: "access_token没有值，不去请求微博数据")
            return
        }
        // 2.006LuhaCrY1jcD8f1e5b55c3UPomuB
        // https://api.weibo.com/2/statuses/home_timeline.json?access_token=2.006LuhaCrY1jcD8f1e5b55c3UPomuB
        let urlString = "2/statuses/home_timeline.json"
        
        // 参数
        var parameters: [String: AnyObject] = [
            "access_token" : access_token
        ]
        
        // 测试
//        parameters["count"] = 1
        
        if since_id > 0 {
            parameters["since_id"] = NSNumber(longLong: since_id)
        } else if max_id > 0 {
            parameters["max_id"] = NSNumber(longLong: max_id - 1)
        }
        
        // 发送请求
        YGNetworkTool.sharedInstance.request(requesMethod.GET, URLString: urlString, parameters: parameters, success: { (_, responseObject) -> Void in
            //                YGPrint(items: "\(responseObject)")
            self.isLoadingStatus = false
            if let result = responseObject as? [String: AnyObject] {
                // 获取所有的微博数据
                if let statuses = result["statuses"] as? [[String: AnyObject]] {
                    // 定义空数组
                    var statusArr = [YGStatus]()
                    
                    // 遍历数组，取出每一个字典
                    for dic in statuses {
                        // 字典转模型
                        let statusModel = YGStatus(dic: dic)
                        // 模型添加到数组
                        statusArr.append(statusModel)
                    }
//                    YGPrint(items: "statusArr: \(statusArr)")
                    // 调用闭包
                    loadStatusCallBack(statuses: statusArr, error: nil)
                }
            }
            
            }, failure: { (_, error) -> Void in
                self.isLoadingStatus = false
//                YGPrint(items: "获取微博数据失败：\(error)")
                loadStatusCallBack(statuses: nil, error: error)
        })
    }
}



























